/*
#------------------------------------------------------------------------
  JA Purity II for Joomla 1.5
#------------------------------------------------------------------------
#Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
#@license - GNU/GPL, http://www.gnu.org/copyleft/gpl.html
#Author: J.O.O.M Solutions Co., Ltd
#Websites: http://www.joomlart.com - http://www.joomlancers.com
#------------------------------------------------------------------------
*/


JAFormController = new Class( { 
	initialize : function( control, options ){
		// options 
		this.options =  Object.extend({ hideRow:true }, options||{ } );
		this.groups = [];	
		var _default = '';
		var groups = this.getGroup( control );
		this._control = control;

		if( $defined(groups) ) {
			groups._parent = this;
			groups.addEvents({'click': function(){
					if (this.tagName.toLowerCase() == 'select') return;					
					groups._parent.update(this.value);
				},
				'change': function() {
					if (this.tagName.toLowerCase() != 'select') return;
					groups._parent.update(this.value);
				}}
			);			
		}
		this.update(this._default);		
	},
	
	update: function(_default){
		if (!this.items) return;
		this.items.each( function( item ) {
			if( item.tagName.toLowerCase() == 'label' ) return;
			if (!_default || (item.id && item.id.test ('-'+_default+'-'))){
				display = '';
				disabled = false;
			}else{
				display = 'none';
				disabled = true;
			}
			
			
			if( this.options.hideRow == true ) { 
				var parent = this.getParentByTagName(item, "tr" );
				if( $defined(parent) ){
					parent.setStyles( {"display":display} );
					if(disabled){
						parent.rel = "jarel";
					}
				}
			}else {
				item.disabled = disabled;
			}
		}.bind(this) );
		
		this.updateHeight ();
	},
	
	getGroup: function (control) {
		var frm = document.forms['adminForm'];
		var obj = frm['params['+control+']'];
		if (!obj) return null;
		var objs;
		if (obj.tagName == 'SELECT') {
			objs = $(obj).getElements('option');
			this._tparent = this.getParentByTagName(obj, 'table');
			this._dparent = this.getParentByTagName(obj, 'div');
		} else {
			if (obj.length < 1) return null;			
			this._tparent = this.getParentByTagName(obj[0], 'table');
			this._dparent = this.getParentByTagName(obj[0], 'div');
			objs = obj = $$(obj);
		}
		objs.each (function(group){
			this.groups.push(group.value);
			if( group.selected || group.checked){ 
				this._default = group.value;
			}
		}.bind(this));
		
		this.items = $( document.adminForm ).getElements("*[id^=params"+control+"-"+"]");
		return obj;
	},
	
	updateHeight: function () {
		if (this._tparent && this._dparent && this._dparent.hasClass('content')) this._dparent.setStyle('height', this._tparent.offsetHeight);
	},
	
	getParentByTagName: function (el, tag) {
		var parent = $(el).getParent();
		while (!parent || parent.tagName.toLowerCase() != tag.toLowerCase()) {
			parent = parent.getParent();
		}
		return parent;
	}
});

var japaramhelpergroups = new Array();

function initjapramhelpergroup(control, options) {
	japaramhelpergroups.push (new JAFormController (control, options));
}

window.addEvent('load', function() {
	document.adminForm.onsubmit = function(){
		japaramhelpergroups.each (function(group){
			if (!group.options.hideRow)	group.update();
		})
	};
	setTimeout(function() {
		//alert('hupdate height');
		japaramhelpergroups.each (function(group){			
			if (group.options.hideRow)	group.updateHeight();
		});
	},400);
});

//window.addEvent ('load', function(){alert('hehe');this.updateHeight ();});

jQuery.noConflict();

// JA Popup
    jQuery(function () {
        jQuery('[alt=bubbleInfo]').each(function () {
            var distance = 10;
            var time = 250;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = jQuery('.trigger', this);
            var info = jQuery('.popup', this).css('opacity', 0);


            jQuery([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;

                    info.css({
                        top: -90,
                        left: -33,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
    });
    
    //-->
    
// Control show/hide Region:
function showHideRegion(imageObject, imageUrl, regionID){
	var trObject = jQuery("table[class=paramlist admintable]").find("tbody tr");
	var display = "block";
	
	jQuery.each( trObject, function(i){
        if( this.innerHTML.indexOf("<h4") >= 0){
        	if( this.innerHTML.indexOf(regionID) > 0 ){
				if( imageObject.innerHTML == "open" ){
		        	display = "none";
		        	jQuery("#desc-"+regionID).hide();
				}else{
		        	display = "block";
		        	jQuery("#desc-"+regionID).show();
				}
			}else{
				if( this.innerHTML.indexOf('open</a>') > 0 ){
		        	display = "block";
				}else{
					display = "none";
				}
		    }
		}else{
			if(display == "none"){
				if( this.style.display != "none" ){
					jQuery(this).hide();
					jQuery(this).attr("rel", "ja");
				}
			}else{
				if( jQuery(this).attr("rel") == "ja" )
					jQuery(this).show();
			}
		}
    });
    
    jQuery("#"+regionID).removeClass("open");
    jQuery("#"+regionID).removeClass("close");
    if( imageObject.innerHTML == "open" ){
    	jQuery("#"+regionID).addClass("close");
    	imageObject.innerHTML = "close";
    }else{
    	jQuery("#"+regionID).addClass("open");
    	imageObject.innerHTML = "open";
    }
}

// Add onmouseover and onmouseout
function addEventToTR(){
	var trObject = jQuery("table[class=paramlist admintable]").find("tbody tr");
	var regionID = "";
	
	jQuery.each( trObject, function(i){
		if( this.innerHTML.indexOf("<h4") >= 0){			
			regionID = jQuery(this).find("h4").attr("id");
		}
		// Add onmouseover event to TR tag
		jQuery(this).attr("title", regionID);
		jQuery(this).bind('mouseover', function(event) {
			activeRegion(this.title);
		});
		
		// Add icon to td
		/*if( this.innerHTML.indexOf("<h4") < 0){
			var td_value = jQuery(this).find("td[class=paramlist_value]");
			if( td_value.html().replace(" ","") != "" )
				td_value.html( td_value.html() + '<a class="help-item" href="#">Help</a>' );
		}*/
	});
}

function activeRegion(regionID){
	var trObject = jQuery("table[class=paramlist admintable]").find("tbody tr h4");
	jQuery.each( trObject, function(i){
		jQuery(this).removeClass("active");
	});
	jQuery("#"+regionID).addClass("active");
}